/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.lom2owl.adapter;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Adapts an Ariadne repository file in order to translate in to OWL. 
 */
public class AriadneAdapter {

    /**
     * This method adapts an Ariadne Repository learningObject in order to 
     * translate it to OWL using the general translation.  
     *  
     */
    public String adaptAriadne(String ariadneFilePath) throws IOException {

        String resultedAriadneFilePath =
                ariadneFilePath.substring(0,
                (ariadneFilePath.length() - 4)) + "Adapted.xml";
        int pos =
                0;
        try {

            BufferedReader bf =
                    new BufferedReader(new FileReader(ariadneFilePath));
            BufferedWriter bw =
                    new BufferedWriter(new FileWriter(resultedAriadneFilePath));
            String sString;

            while ((sString =
                    bf.readLine()) != null) {

                String requiredText1 =
                        "BEGIN:VCARD";
                String newString1 =
                        "BEGIN:VCARD";
                if (sString.indexOf(requiredText1) > -1) {
                    pos =
                            sString.indexOf(requiredText1);
                    sString =
                            "" + (sString.substring(0, pos) + newString1 +
                            sString.substring((pos + 7), sString.length()));
                }

                String requiredText2 =
                        "2.1";
                String newString2 =
                        "3.0";
                while (sString.indexOf(requiredText2) > -1) {
                    pos =
                            sString.indexOf(requiredText2);
                    sString =
                            "" + (sString.substring(0, pos) + newString2 +
                            sString.substring((pos + 3), sString.length()));
                }

                String requiredText3 =
                        "\\n";
                char newString3 =
                        '\n';
                while (sString.indexOf(requiredText3) > -1) {
                    pos =
                            sString.indexOf(requiredText3);
                    sString =
                            "" + (sString.substring(0, pos) + newString3 +
                            sString.substring((pos + 2), sString.length()));
                }

                String requiredText4 =
                        "FN:";
                String newString4 =
                        "FN:";
                String sC =
                        sString;
                int contadorVeces =
                        0;

                while (sC.indexOf(requiredText4) > -1) {
                    sC =
                            sC.substring(sC.indexOf(requiredText4) +
                            requiredText4.length(), sC.length());
                    contadorVeces++;
                }

                int i =
                        0;
                String sC1 =
                        sString;
                String sC2 =
                        "";

                while ((sC1.indexOf(requiredText4) > -1) && (i <= contadorVeces)) {
                    pos =
                            sC1.indexOf(requiredText4);
                    sC1 =
                            "" + (sC1.substring(0, pos) + newString4 +
                            sC1.substring((pos + 3), sC1.length()));

                    sC2 =
                            sC2 + (sC1.substring(0, pos) + newString4);
                    sC1 =
                            sC1.substring((pos + 4), sC1.length());

                    i++;
                }

                sC2 =
                        sC2 + sC1.substring(0, sC1.length());
                sString =
                        sC2;

                String requiredText5 =
                        " VERSION";
                String newString5 =
                        "VERSION";
                while (sString.indexOf(requiredText5) > -1) {
                    pos =
                            sString.indexOf(requiredText5);
                    sString =
                            "" + (sString.substring(0, pos) + newString5 +
                            sString.substring((pos + 8), sString.length()));
                }

                String requiredText6 =
                        " N";
                String newString6 =
                        "N";
                while (sString.indexOf(requiredText6) > -1) {
                    pos =
                            sString.indexOf(requiredText6);
                    sString =
                            "" + (sString.substring(0, pos) + newString6 +
                            sString.substring((pos + 2), sString.length()));
                }

                String requiredText7 =
                        " EMAIL";
                String newString7 =
                        "EMAIL";
                while (sString.indexOf(requiredText7) > -1) {
                    pos =
                            sString.indexOf(requiredText7);
                    sString =
                            "" + (sString.substring(0, pos) + newString7 +
                            sString.substring((pos + 7), sString.length()));
                }

                String requiredText8 =
                        " TEL";
                String newString8 =
                        "TEL";
                while (sString.indexOf(requiredText8) > -1) {
                    pos =
                            sString.indexOf(requiredText8);
                    sString =
                            "" + (sString.substring(0, pos) + newString8 +
                            sString.substring((pos + 4), sString.length()));
                }

                String requiredText9 =
                        " ORG";
                String newString9 =
                        "ORG";
                while (sString.indexOf(requiredText9) > -1) {
                    pos =
                            sString.indexOf(requiredText9);
                    sString =
                            "" + (sString.substring(0, pos) + newString9 +
                            sString.substring((pos + 4), sString.length()));
                }

                String requiredText10 =
                        "END:VCARD";
                String newString10 =
                        "END:VCARD" + '\n';
                while (sString.indexOf(requiredText10) > -1) {
                    pos =
                            sString.indexOf(requiredText10);
                    sString =
                            "" + (sString.substring(0, pos) + newString10 +
                            sString.substring((pos + 5), sString.length()));
                }

                bw.write(sString + '\n');

            }

            bw.close();

        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return resultedAriadneFilePath;
    }

    public static void main(String args[]) throws IOException {
        AriadneAdapter a =
                new AriadneAdapter();
        a.adaptAriadne(args[0]);
    }
}
